<template>
  <div class="jsbos-common-layout user_box">
    <div class="jsbos-common-layout-center">
      <div class="jsbos_search">
        <el-form @submit.native.prevent label-width="90px">
          <el-form-item label="名字">
            <el-input
              style="width: 100%"
              v-model="query.userName"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input
              style="width: 100%"
              v-model="query.userPhone"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="search()"
              >查询</el-button
            >
            <el-button icon="el-icon-refresh-right" @click="reset()"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="jsbos-common-layout-main jsbos-flex-main">
        <div class="jsbos-common-head">
          <div>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle()"
              >新增
            </el-button>
            <el-button
              type="primary"
              icon="el-icon-upload2"
              @click="uploadForm()"
              >导入</el-button
            >
            <el-button type="text" icon="el-icon-download" @click="exportData()"
              >导出
            </el-button>

            <el-button
              type="text"
              style="font-size: 12px"
              icon="icon-ym icon-ym-nav-prev"
              @click="goBackPre()"
              >返回
            </el-button>
          </div>
          <div class="jsbos-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JSBOS-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
            <screen-full isContainer />
            <el-tooltip effect="dark" content="列设置" placement="top">
              <el-link
                icon="icon-ym icon-ym-options jsbos-common-head-icon"
                :underline="false"
                @click="() => this.$refs.jsbosTableRef.showDrawer()"
              />
            </el-tooltip>
          </div>
        </div>
        <jsbos-table
          v-loading="listLoading"
          :data="list"
          ref="jsbosTableRef"
          @sort-change="sortChange"
          has-c
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            prop="userName"
            label="名字"
            width="0"
            align="left"
          />
          <el-table-column
            prop="userPhone"
            label="手机号"
            width="0"
            align="left"
          />
          <el-table-column
            prop="pass"
            label="是否参与抽奖"
            width="0"
            align="left"
          />
          <el-table-column
            prop="creatorUserId"
            label="创建人"
            width="0"
            align="left"
          />
          <el-table-column
            prop="creatorTime"
            label="创建时间"
            width="0"
            align="left"
          />
          <el-table-column label="操作" fixed="right" width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="addOrUpdateHandle(scope.row.id)"
                >编辑
              </el-button>
              <el-button
                type="text"
                class="jsbos-table-delBtn"
                @click="handleDel(scope.row.id)"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </jsbos-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <!-- 导入 -->
    <ImportForm
      v-if="importFormVisible"
      ref="importUserForm"
      @refresh="freshimport"
    />
    <JSBOS-Form v-if="formVisible" ref="JSBOSForm" @refresh="refresh" />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
    <Detail
      v-if="detailVisible"
      ref="Detail"
      @refresh="detailVisible = false"
    />
  </div>
</template>

<script>
import ImportForm from "./ImportForm";
import request from "@/utils/request";
import JSBOSForm from "./Form";
import ExportBox from "./ExportBox";
import Detail from "./Detail";

export default {
  name: "CmsAwardUser",
  components: { JSBOSForm, ExportBox, Detail, ImportForm },
  data() {
    return {
      // 导入 显示/隐藏
      importFormVisible: false,
      activityId: "",
      detailVisible: false,
      query: {
        activityId: undefined,
        userName: undefined,
        userPhone: undefined,
        remark: undefined,
      },
      treeProps: {
        children: "children",
        label: "fullName",
        value: "id",
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [
        { prop: "userName", label: "名字" },
        { prop: "userPhone", label: "手机号" },
        { prop: "pass", label: "是否抽奖" },
        { prop: "remark", label: "备注" },
      ],
    };
  },
  computed: {
    menuId() {
      return this.$route.meta.modelId || "";
    },
  },
  created() {
    var activityId = this.$route.query.activityId;
    this.activityId = activityId;
    // this.query.activityId = activityId;
    this.initData();
  },
  methods: {
    goDetail(id) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.Detail.init(id);
      });
    },
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    initData() {
      this.listLoading = true;
      this.query.activityId = this.activityId;
      let _query = {
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      request({
        url: `/api/cms/CmsAwardUser/getList`,
        method: "post",
        data: _query,
      }).then((res) => {
        var _list = [];
        for (let i = 0; i < res.data.list.length; i++) {
          let _data = res.data.list[i];
          _list.push(_data);
        }
        this.list = _list;
        this.total = res.data.pagination.total;

        this.listLoading = false;
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/cms/CmsAwardUser/${id}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    handleSelectionChange(val) {
      const res = val.map((item) => item.id);
      this.multipleSelection = res;
    },
    handleBatchRemoveDel() {
      if (!this.multipleSelection.length) {
        this.$message({
          type: "error",
          message: "请选择一条数据",
          duration: 1500,
        });
        return;
      }
      const ids = this.multipleSelection.join();
      this.$confirm("您确定要删除这些数据吗, 是否继续？", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/cms/CmsAwardUser/batchRemove/${ids}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.JSBOSForm.init(id, isDetail, this.activityId);
      });
    },
    exportData() {
      this.exportBoxVisible = true;
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList);
      });
    },
    download(data) {
      let query = {
        ...data,
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      request({
        url: `/api/cms/CmsAwardUser/Actions/Export`,
        method: "GET",
        data: query,
      }).then((res) => {
        if (!res.data.url) return;
        this.jsbos.downloadFile(res.data.url);
        this.$refs.ExportBox.visible = false;
        this.exportBoxVisible = false;
      });
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
        activityId: this.activityId,
      };

      this.initData();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      if (isrRefresh) this.reset();
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.search();
    },
    //返回前一页
    goBackPre() {
      this.$router.back();
    },

    //导入
    uploadForm() {
      this.importFormVisible = true;
      this.$nextTick(() => {
        this.$refs.importUserForm.init(this.activityId);
      });
    },
    //刷新
    freshimport() {
      this.initData();
    },
  },
};
</script>

<style scoped lang="scss">
.user_box {
  >>> .icon-ym-nav-prev {
    font-size: 12px;
  }
}
</style>
